<template>
  <div class="register">
    <div class="register-container">
        <div class="title">
            <h3>立即注册</h3>
            <el-button @click="goLogin()" >去登陆</el-button>
        </div>
        <el-form :model="Regiterform" status-icon :rules="rules" ref="Regiterform"   class="form">
    <el-form-item label="" prop="username">
        <el-input v-model="Regiterform.username" type="text" placeholder="请输入用户名" prefix-icon="el-icon-user" autocomplete="off"></el-input>
    </el-form-item>
  
     <el-form-item label="" prop="password">
        <el-input type="password" v-model="Regiterform.password" autocomplete="off" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
     </el-form-item>
  
     <el-form-item label="" prop="checkPass">
         <el-input type="password" v-model="Regiterform.checkPass" autocomplete="off" placeholder="请再次输入密码"
         prefix-icon="el-icon-lock"></el-input>
     </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="submitForm('Regiterform')" style="width:100%;background-color: #505354;">立即注册</el-button>
  </el-form-item>
</el-form>
    </div>
  </div>
</template>

<script>

export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.Regiterform.checkPass !== '') {
            this.$refs.Regiterform.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.Regiterform.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        Regiterform: {
          password: '',
          checkPass: '',
          username: ''
        },
        rules: {
          username: [
            { required:true, message:"请输入你的用户名", trigger:'blur' },
            { min: 2,max: 9,message:"长度2到9个字符",trigger:'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(Regiterform){
        this.axios.post('http://localhost:8088/save',this.Regiterform)
        .then((response)=>{
          console.log(response);
          let data=response.data;
          if(data=='success'){
            //成功后打印
          this.Regiterform={};
          this.$message({
          message: '注册成功!',
          type: 'success'
          });
          }
        })
        
      },
      goLogin(){
          this.$router.push({path:'/login'})
      }
    }
}
</script>

<style lang="less">
    .register{
        height: 660px;
        width: 100%;
        position: relative;
        .register-container{
            width:500px;
            height:400px;
            margin: auto;
            position: absolute;  
            top: 0; left: 0; bottom: 0; right: 0; 
            box-shadow: 0 0 25px #cac6c6;
            border-radius: 10px;
            .title{
                margin-top: 20px;
                margin-left: 180px;
                font-size: 20px;
                display: flex;
            }
            .form{
                padding: 20px;
            }
        }
    }
</style>